#include "Functions/String.h"
using namespace std;

/// Remove all leading and trailing whitespaces from a string.
/// @todo This function already exists as such in `TString`. Migrate from `string` to `TString`.
string& BOSS_Afterburner::String::Trim(string& input)
{
  while(input.front() == ' ' || input.front() == '\t')
    input.erase(0, 1);
  while(input.back() == ' ' || input.back() == '\t')
    input.pop_back();
  return input;
}

/// Remove all leading and trailing characters of type `c` from a string.
string& BOSS_Afterburner::String::Trim(string& input, char c)
{
  while(input.front() == c)
    input.erase(0, 1);
  while(input.back() == c)
    input.pop_back();
  return input;
}

/// Remove all leading and trailing whitespaces from a string.
/// @todo This function already exists as such in `TString`. Migrate from `string` to `TString`.
TString& BOSS_Afterburner::String::Trim(TString& input)
{
  while(input.BeginsWith(" ") || input.BeginsWith("\t"))
    input.Remove(0, 1);
  while(input.EndsWith(" ") || input.EndsWith("\t"))
    input.Resize(input.Length() - 1);
  return input;
}

/// Remove all leading and trailing characters of type `c` from a string.
TString& BOSS_Afterburner::String::Trim(TString& input, char c)
{
  while(input.BeginsWith(c))
    input.Remove(0, 1);
  while(input.EndsWith(Form("%c", c)))
    input.Resize(input.Length() - 1);
  return input;
}

/// Check if a string starts with a comment identifier (such as `//` in the case of `C++`).
const bool BOSS_Afterburner::String::IsComment(string input)
{
  Trim(input);
  input = input.substr(0, 2);
  if(!input.compare("//")) return true; // C++ line comment
  if(!input.compare("/*")) return true; // C++ begin block comment
  if(input.front() == '*') return true; // C++ continue/end block comment
  if(input.front() == '#') return true; // bash comment
  if(input.front() == '%') return true; // LaTeX comment
  return false;
}

const bool BOSS_Afterburner::String::IsComment(const TString& input)
{
  return IsComment((string)(input.Data()));
}

TString BOSS_Afterburner::String::NoMainDir(TString path)
{
  auto pos = path.First('/');
  if(pos >= 0) path.Remove(0, pos + 1);
  return path;
}

TString BOSS_Afterburner::String::NoExtension(TString path)
{
  auto pos = path.Last('.');
  if(pos >= 0) path.Resize(pos);
  return path;
}